<?php
namespace app\admin\controller;

//分类管理
class Brand extends Common
{
	//分类列表
    public function brand_list()
    {
		$where=array('a.gb_id'=>array('gt',0));
        $name=Request()->param('name');
		//名称
		$status=Request()->param('status');
		//状态
        $start_time=strtotime(Request()->param('start_time'));
        $end_time=strtotime(Request()->param('end_time'));
        $page=Request()->param('page');
        $size=Request()->param('size');
        if($page<1){
            $page=1;
        }
        if($size<15){
            $size=15;
        }
        $now=($page-1)*$size;
		if($name!=''){
			$where['a.gb_name']=array('like','%'.$name.'%');
		}
		if($status>0){
			if($status==1){
				$where['a.gb_status']=1;
			}else{
				$where['a.gb_status']=2;
			}
			
		}
        $where_time=array(array('gt',0),array('gt',0));
        if($start_time>0){
            $where_time[]=array('egt',$start_time);
        }else{
            $start_time='';
        }
        if($end_time>0){
            $where_time[]=array('elt',$end_time);
        }else{
            $end_time='';
        }
        $where['a.gb_create_time']=$where_time;
        $brand=db('goods_brand a')
            ->field("a.*,count(b.g_id) as g_number")
            ->join("fr_goods b","a.gb_id = b.gb_id","left")
			->order('a.gb_sort desc,a.gb_status')
			->where($where)
            ->limit("$now,$size")
            ->group("a.gb_id")
			->select();
		//标签列表
        $count=db('goods_brand a')
            ->where($where)
            ->count();
        //等级信息
        $page_str=page($page,$size,$count,count($brand));//分页
        $this->view->assign('page_str',$page_str);
		$this->view->assign('data',$brand);
		$this->assign('status',$status);
		$this->assign('name',$name);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('page',$page);
        $this->assign('size',$size);
        $this->assign('count',$count);
        return $this->view->fetch();

    }
	//添加品牌
    function add_brand(){
        if(request()->isGet()){
            $type=db('goods_type a')
                ->where(array('a.gt_status'=>1))
                ->select();
            //标签列表
            $type1=array();
            $type2=array();
            $type3=array();
            foreach($type as $k=>$v){
                if($v['gt_type']==1){
                    $type1[]=$v;
                }
                if($v['gt_type']==2){
                    $type2[]=$v;
                }
                if($v['gt_type']==3){
                    $type3[]=$v;
                }
            }
            $this->assign('type1',$type1);
            $this->assign('type2',$type2);
            $this->assign('type3',$type3);
            return $this->view->fetch();
        }else if(request()->isAjax()) {
            $data = request()->post();
            $data['gb_name'] = trim($data['gb_name']);
            if ($data['gb_name'] == '') {
                return return_json(array('code' => 0, 'msg' => '请输入品牌名称'), '添加品牌');
            }
            $brand=db('goods_brand')->where(array('gb_name'=>$data['gb_name']))->find();
            if(!empty($brand)){
                return return_json(array('code'=>0,'msg'=>'已有相同名称的品牌'),'添加品牌');
            }
            if($data['gb_sort']<=0){
                $data['gb_sort']=0;
            }
            $type=$data['type'];
            unset($data['type']);
            if(!empty($type)){
                $type_str=','.implode(',',$type).',';
            }else{
                $type_str='';
            }
            $data['gb_type_ids']=$type_str;
            if($data['gb_status']!=1){
                $data['gb_end_time']=time();
            }
            $data['gb_create_time']=time();
            $data['gb_add_user_id']=session('uid');
            $data['gb_add_user_name']=session('user.real_name');
            $res=db('goods_brand')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'品牌添加成功'),'添加品牌');
            }else{
                return return_json(array('code'=>0,'msg'=>'品牌添加失败'),'添加品牌');
            }
        }
    }
    //修改品牌
    function rebrand(){
        if(request()->isGet()){
            $id=request()->get('id');
            $brand=db('goods_brand')
                ->where(array('gb_id'=>$id))
                ->find();
            $type_ids=explode(',','0'.$brand['gb_type_ids'].'0');
            $type=db('goods_type a')
                ->where(array('a.gt_status'=>1))
                ->select();
            //标签列表
            $type1=array();
            $type2=array();
            $type3=array();
            foreach($type as $k=>$v){
                if($v['gt_type']==1){
                    $type1[]=$v;
                }
                if($v['gt_type']==2){
                    $type2[]=$v;
                }
                if($v['gt_type']==3){
                    $type3[]=$v;
                }
            }
            $this->assign('type1',$type1);
            $this->assign('type2',$type2);
            $this->assign('type3',$type3);
            $this->assign('brand',$brand);
            $this->assign('type_ids',$type_ids);
            return $this->view->fetch();
        }else if(request()->isAjax()) {
            $data = request()->post();
            $id=$data['id'];
            unset($data['id']);
            $data['gb_name'] = trim($data['gb_name']);
            if ($data['gb_name'] == '') {
                return return_json(array('code' => 0, 'msg' => '请输入品牌名称'), '修改品牌');
            }
            $brand=db('goods_brand')->where(array('gb_name'=>$data['gb_name'],'gb_id'=>array('neq',$id)))->find();
            if(!empty($brand)){
                return return_json(array('code'=>0,'msg'=>'已有相同名称的品牌'),'修改品牌');
            }
            if($data['gb_sort']<=0){
                $data['gb_sort']=0;
            }
            $type=$data['type'];
            unset($data['type']);
            if(!empty($type)){
                $type_str=','.implode(',',$type).',';
            }else{
                $type_str='';
            }
            $data['gb_type_ids']=$type_str;
            if($data['gb_status']!=1){
                $data['gb_end_time']=time();
            }
            $data['gb_update_time']=time();
            $res=db('goods_brand')->where(array('gb_id'=>$id))->update($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'品牌修改成功'),'修改品牌');
            }else{
                return return_json(array('code'=>0,'msg'=>'品牌修改失败'),'修改品牌');
            }
        }
    }
    //禁用品牌
    function brand_del(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('goods_brand')
                ->where(array('gb_id'=>$id))
                ->update(array('gb_status'=>2,'gb_end_time'=>time()));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用品牌');
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用品牌');
            }
        }
    }
    //启用品牌
    function brand_use(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('goods_brand')
                ->where(array('gb_id'=>$id))
                ->update(array('gb_status'=>1,'gb_end_time'=>time()));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'启用成功'),'启用品牌');
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败'),'启用品牌');
            }
        }
    }
    //导入品牌
    public function add_brand_csv(){
        if(request()->isGet()){
            return $this->view->fetch();
        }else if(request()->isAjax()) {
            set_time_limit(0);
            $file = request()->file('csv_file');
            if (!$file) {
                return return_json(array('code' => 0, 'msg' => '请选择文件'), '导入品牌');
            }
            //以下将模板传入第三方
//            $upload = new Upload();
//            $return=$upload->upload($file);
//            if($return['code']!=1){
//                return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传模板');
//            }else{
//                $data['g_img']=$return['url'];
//            }
//            echo $data['g_img'];die;
            //http://sczxyx-mall.oss-cn-chengdu.aliyuncs.com/a5e6c20190821113542811737.csv
            //模板地址
            //以上将模板传入第三方
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
            if ($ext != 'csv' && $ext != 'CSV') {
                return return_json(array('code' => 0, 'msg' => '只支持csv文件'), '导入品牌');
            }
            $csv_file = fopen($file->getInfo('tmp_name'), 'r');
            //打开文件
            while ($data = fgetcsv($csv_file, 1000, ",")) {
                $data = eval('return ' . iconv('gbk', 'utf-8', var_export($data, true)) . ';');//防止乱码
                //读取有保单号的数据
                $list[] = $data;

            }
            //将文件内容读取未数组
            unset($list[0]);//去除第一行
//            $system=db('system')->find();
            //系统设置
            if(empty($list)){
                return return_json(array('code' => 0, 'msg' => '没有数据'), '导入品牌');
            }
            $name_str='';
            foreach($list as $v){
                if($v[0]!=''){
                    $name_str.=$v[0].',';
                }
            }
            $name_str=substr($name_str,0,-1);
            $have=db('goods_brand')
                ->field("gb_name")
                ->where(array('gb_name'=>array('in',$name_str)))
                ->select();
            $have_brand=array();
            foreach($have as $v){
                $have_brand[]=$v['gb_name'];
            }
            $brand=array();
            foreach($list as $v) {
                if ($v[0]!='' && !in_array($v[0],$have_brand)){
                    $brand[]=array('gb_name'=>$v[0],'gb_sort'=>$v[2],'gb_status'=>$v[1]=='启用'?1:2,'gb_create_time'=>time(),'gb_add_user_id'=>session('uid'),'gb_add_user_name'=>session('user.real_name'));
                }
            }
            if(!empty($brand) && count($brand)>0){
                db('goods_brand')->insertAll($brand);
            }
            return return_json(array('code' => 1, 'msg' => '品牌添加成功'), '导入品牌');
        }
    }
}
